#pragma once

#ifndef _POINTER_HPP_
#define _POINTER_HPP_

class Counter
{
public:
	Counter() : counter_( 0 ) {}

	void _incCounter() { ++counter_; };
	int _decCounter() { return --counter_; };
private:
	int counter_;
};

template < typename Type >
class Pointer
{
public:
	Pointer() : obj_( 0 ) {}
	Pointer( Type * obj ) {
		obj_ = obj;
		if ( obj_ ) {
			obj_->_incCounter();
		}
	}
	Pointer( Pointer &pointer ) {
		obj_ = pointer.obj_;
		if ( obj_ ) {
			obj_->_incCounter();
		}
	}
	~Pointer() {
		if ( obj_ && !obj_->_decCounter() ) {
			delete obj_;
		}
	}

	Type & operator * () { return *obj_; }
	Type * operator -> () { return obj_; }

	Pointer & operator = ( Pointer & ptr ) {
		if ( obj_ && !obj_->_decCounter() ) {
			delete obj_;
		}
		obj_ = ptr.obj_;
		if ( obj_ ) {
			obj_->_incCounter();
		}
	}

	operator void * () {
		return obj_;
	}
	
private:
	Type * obj_;
};


#endif
